#pragma once

#include "qt_graphics_shape_point.h"
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsItem>
#include <QPainter>
#include <QPen>


using namespace std;
#include "math.h"


class GraphicsShapeCirce : public QObject,public QAbstractGraphicsShapeItem
{
    Q_OBJECT
public:

    QPoint m_qpoint_strat;
    QPoint m_qpoint_end;

    int m_ring_width = 20;

    bool m_move=false;
    QPoint  m_pressedPos ;
    QPoint  m_startPos ;

    vector<QPoint> m_points;

    QPen m_pen;
    int  m_point_half_size=8;
    bool  m_in = false;

    int m_editingVertex =-1;
    QPoint m_dragStartPosition;

signals:
    void sendMessage(QPointF pos);
public:
    GraphicsShapeCirce(qreal x, qreal y, qreal width, qreal height,QColor color)
    {
        //true hoverEnterEvent hoverLeaveEvent start
        this->setAcceptHoverEvents(true);

        this->setFlags(QGraphicsItem::ItemIsSelectable |
                       QGraphicsItem::ItemIsMovable |
                       QGraphicsItem::ItemIsFocusable);

        setFlag(QGraphicsItem::ItemSendsGeometryChanges);
                setCacheMode(QGraphicsItem::DeviceCoordinateCache);

        this->setCursor(Qt::PointingHandCursor);

        m_points.push_back(QPoint(x,y));
        m_points.push_back(QPoint(x+width,y+height));

        m_pen.setColor(color);
        m_pen.setWidth(5);
        m_pen.setStyle(Qt::SolidLine);
    }

    QRectF boundingRect() const
    {
        return QRectF(m_points[0].x()-m_point_half_size,m_points[0].y()-m_point_half_size,m_points[1].x()-m_points[0].x()+m_point_half_size*2,m_points[1].y()-m_points[0].y()+m_point_half_size*2);
    }

//    QPainterPath  shape() const {
//        QPainterPath   path;
//        path.addEllipse(m_points[0].x(),m_points[0].y(),m_points[1].x()-m_points[0].x(),m_points[1].y()-m_points[0].y());
//        return path;
//    }
    virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    {
        Q_UNUSED(option);
        Q_UNUSED(widget);
        painter->setPen(m_pen);
        QPainterPath   path;
        path.addEllipse(m_points[0].x(),m_points[0].y(),m_points[1].x()-m_points[0].x(),m_points[1].y()-m_points[0].y());
        path.addEllipse(m_points[0].x()+m_ring_width,m_points[0].y()+m_ring_width,m_points[1].x()-m_points[0].x()-m_ring_width*2,m_points[1].y()-m_points[0].y()-m_ring_width*2);

        painter->drawPath(path);
//        if(m_in)
//        {
            QPainterPath   vertex_path;
            for(auto obj : m_points)
                    vertex_path.addRect(QRect(obj.x() - m_point_half_size, obj.y() - m_point_half_size, m_point_half_size*2, m_point_half_size*2));
            painter->drawPath(vertex_path);
//        }

    }

    virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event)
    {
//        m_pen.setColor(Qt::green);
//        m_pen.setStyle(Qt::SolidLine);
        m_in =true;



        QGraphicsItem::hoverEnterEvent(event);
    }

    virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
    {
        m_in =false;
//        m_pen.setColor(Qt::blue);
//        m_pen.setStyle(Qt::SolidLine);
        QGraphicsItem::hoverLeaveEvent(event);
    }

    virtual void hoverMoveEvent(QGraphicsSceneHoverEvent* event)
    {
        QPointF pos = event->pos();
        emit sendMessage(pos);
    }




            // 如果未点击到顶点，则可能开始新的交互（此处示例中未实现）


void mousePressEvent(QGraphicsSceneMouseEvent *event) override {

        for (int i = 0; i < m_points.size(); ++i) {
            if (abs(m_points[i].x()-event->pos().x()<=m_point_half_size && abs(m_points[i].y()-event->pos().y())<=m_point_half_size))
            {
                m_editingVertex = i;
                m_dragStartPosition = m_points[i];
                update(); // 触发重绘
                return;
            }
        }
        m_move = true;
        m_pressedPos  = event->scenePos().toPoint();
        m_startPos = this->pos().toPoint();
            // 在此处添加按下鼠标时的处理代码
        }

void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override {
if (m_editingVertex != -1) {
                       // 更新正在编辑的顶点位置
                QPoint pos = event->pos().toPoint();
//                QPoint poss = event->pos().toPoint()  -  (event->pos().toPoint() - m_dragStartPosition);
               QPoint poss =  pos ;


                       m_points[m_editingVertex] =poss;
                       update(); // 触发重绘
                   }
else if(m_move) {
    // 获取场景坐标和本地坐标
        QPoint pos = event->scenePos().toPoint();

        // 计算偏移
        qreal xInterval = pos.x() - m_pressedPos .x();
        qreal yInterval = pos.y() - m_pressedPos .y();

        // 设置在场景中位置
//        this->setPos(m_startPos + QPointF(xInterval, yInterval));
         this->setPos(m_startPos + QPointF(xInterval, yInterval));
        this->update();
}

}

        void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override {
            m_move=false;
            if (m_editingVertex != -1) {
                       m_editingVertex = -1; // 停止编辑
                       update(); // 触发重绘
                   }
            // 在此处添加释放鼠标时的处理代码
        }


//    void draw()
//    {
//        delete m_path;
//        m_path=new m_path()
//        for(auto obj : m_points)
//        {
//            m_vertex_path->addRect(QRect(obj.x() - m_point_size / 2, obj.y() - m_point_size / 2, m_point_size, m_point_size));
//        }
//    }

//    void drawVertex()
//    {
//        for(auto obj : m_points)
//        {
//            m_vertex_path->addRect(QRect(obj.x() - m_point_size / 2, obj.y() - m_point_size / 2, m_point_size, m_point_size));
//        }
//    }
};

